const express = require("express");
const dbRouter = express.Router();
const url = require("url");
const connectMySQL = require("../utils/config.js");

dbRouter.get("/registry", (req, res) => {
  let username = url.parse(req.url, true).query.username;
  let password = url.parse(req.url, true).query.password;
  const sql = "insert into user values (null,?,?)";
  connectMySQL(sql, [username, password], (result) => {
    // console.log(result);
    if (result.affectedRows > 0) {
      res.send({
        returnCode: 200,
        data: "注册成功"
      })
    }
  })
})

dbRouter.post("/login", (req, res) => {
  const { username, password } = req.body;
  const sql = "select * from user where name=? and age=?";
  connectMySQL(sql, [username, password], (result) => {
    console.log(result);
    if (result.length > 0) {
      res.send({
        returnCode: 200,
        data: result
      })
    } else {
      res.send({
        returnCode: 400,
        data: "登录失败"
      })
    }

  })

})


module.exports = dbRouter;